#include "Message_Processing.h"
void Init_Msg_FCH(MMSG **MMsg_pp)
{
	
//	DMP_Ext_DIUC *ded=NULL;
	
	(*MMsg_pp) =(MMSG *)malloc(sizeof(MMSG));
	(*MMsg_pp)->fch = (FCH *)calloc(sizeof(FCH),1);

	//Initail FCH Elements
	(*MMsg_pp)->fch->UB_SBCH = 0x1;
	(*MMsg_pp)->fch->RP_CD_IND = 0x2;
	(*MMsg_pp)->fch->CD_IND = 0x3;
	(*MMsg_pp)->fch->DMP_LEN = 0x10;
}

void Form_FCH(MMSG **mmsg_pp,MsgQ **MsgPtr)
{
    Insert_MsgQ_DList(MsgPtr,(*mmsg_pp)->fch->UB_SBCH,L_UB_SBCH);
    Insert_MsgQ_DList(MsgPtr,0,1);
	Insert_MsgQ_DList(MsgPtr,(*mmsg_pp)->fch->RP_CD_IND,L_RP_CD_IND);
	Insert_MsgQ_DList(MsgPtr,(*mmsg_pp)->fch->CD_IND,L_CD_IND);
	Insert_MsgQ_DList(MsgPtr,(*mmsg_pp)->fch->DMP_LEN,L_DMP_LEN);
	Insert_MsgQ_DList(MsgPtr,0,4);
}
void DeForm_FCH(BUFFER **buf_pp,FCH **fch_pp)
{
	uint64 tmp;
	*fch_pp = (FCH *)malloc(sizeof(FCH));
    DeFormat2Val(buf_pp,(uint64 *)&(*fch_pp)->UB_SBCH,L_UB_SBCH);                  
    DeFormat2Val(buf_pp,&tmp,1);                                             
	DeFormat2Val(buf_pp,(uint64 *)&(*fch_pp)->RP_CD_IND,L_RP_CD_IND);              
	DeFormat2Val(buf_pp,(uint64 *)&(*fch_pp)->CD_IND,L_CD_IND);                    
	DeFormat2Val(buf_pp,(uint64 *)&(*fch_pp)->DMP_LEN,L_DMP_LEN);                  
	DeFormat2Val(buf_pp,&tmp,4);	  
	PrintMsg("----------","Decode FCH","----------");
	PrintMsgVal("FCH","UB_SBCH",(*fch_pp)->UB_SBCH);
	PrintMsgVal("FCH","RP_CD_IND",(*fch_pp)->RP_CD_IND);
	PrintMsgVal("FCH","CD_IND",(*fch_pp)->CD_IND);
	PrintMsgVal("FCH","DMP_LEN",(*fch_pp)->DMP_LEN);

}
void Compare_FCH(MMSG *mmsg_gen_p ,MMSG *mmsg_get_p)
{
	BUFFER *buf_gen_p=NULL;
	BUFFER *buf_get_p=NULL;
	MsgQ *Msg_gen_p=NULL;
	MsgQ *Msg_get_p=NULL;
	int result;

	Form_FCH(&mmsg_gen_p,&Msg_gen_p);
	buf_gen_p = FormatMsgQ2Buf(Msg_gen_p);

	Form_FCH(&mmsg_get_p,&Msg_get_p);
	buf_get_p = FormatMsgQ2Buf(Msg_get_p);
	result = memcmp (buf_get_p->buffer,buf_gen_p->buffer,buf_gen_p->bitoffset);
	printf("The compare result of FCH is %d\n",!result);

}

void Test_FCH()
{
	MsgQ *MsgPtr=NULL;
	MMSG *mmsg_gen_p=NULL;//generate
	MMSG *mmsg_ope_p=NULL;//operate
	MMSG *mmsg_get_p=NULL;//get
	BUFFER *buf_p=NULL;
	

    mmsg_get_p =(MMSG *)malloc(sizeof(MMSG));

	
	Init_MsgQ(&MsgPtr);
	Init_Buffer(&buf_p);

	Init_Msg_FCH(&mmsg_gen_p);
	Init_Msg_FCH(&mmsg_ope_p);    

	Form_FCH(&mmsg_ope_p,&MsgPtr);
	buf_p = FormatMsgQ2Buf(MsgPtr);
	buf_p->base =buf_p->buffer;
	buf_p->bitoffset=0;
	DeForm_FCH(&buf_p,&mmsg_get_p->fch);
    Compare_FCH(mmsg_gen_p,mmsg_get_p);
}

